package com.hs.mesj.socket;


import com.alibaba.fastjson.JSON;
import com.config.ContantValue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerThread implements Runnable {
    public  static Log log = LogFactory.getLog(ServerThread.class);
    public Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            while (true) {
                String str = br.readLine();
                log.info("socketServer服务器收到" + str);
            //   收到消息 发送给每一个客户端 (借助websocket)
                String msg = null;
                //同步本地map
                String push = null;
                if(!str.isEmpty()){


                    //处理接收到的数据 socket
                    String sttt = str.substring(1,str.length()-1);
                    String wq[] = sttt.split("\\+");
                    String w = wq[0];
                    String q = wq[1];
                    if(q.equals(0) ||q.equals("0")){
                        ContantValue.distValue.put("0+1",q);
                        push = "0+1|"+q;
                    }else if(q.equals(1) || q.equals("1")){
                        ContantValue.distValue.put("0+1",q);
                        push = "0+1|"+q;
                    }else if(q.equals(2)||q.equals("2")||q.equals(3)||q.equals("3")||q.equals(4)||q.equals("4")||q.equals(5)
                            ||q.equals("5")||q.equals(6)||q.equals("6")||q.equals(7)||q.equals("7")||q.equals(8)||q.equals("8")){
                        ContantValue.distValue.put(q,w);
                        push = q+"|"+w;
                    }else if(q.equals(9) ||q.equals("9")|| q.equals("A")){
                        ContantValue.distValue.put("9+A",q);
                        push = "9+A|"+q;
                    }else if(q.equals("B") || q.equals("C")){
                        ContantValue.distValue.put("B+C",q);
                        push = "B+C|"+q;
                    }else if(q.equals("D") || q.equals("E")){
                        ContantValue.distValue.put("D+E",q);
                        push = "D+E|"+q;
                    }else if(q.equals("F") || q.equals("10")|| q.equals(10)){
                        ContantValue.distValue.put("F+10",q);
                        push = "F+10|"+q;
                    }else{
                        push = w+"|"+q;
                    }

                }
                if(!str.isEmpty()){
                    String jsonObject = JSON.toJSONString(ContantValue.distValue);
                    log.info(jsonObject);
                    log.info(push);
                    if(push!=null){
                        WebSocketServer.sendInfoAll(push);
                    }else{
                        WebSocketServer.sendInfoAll("11111|000000");
                    }
                }
                pw.print("服务器应答："+str);
                pw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(ContantValue.distValue.toString());
        System.out.println(ContantValue.distValue.toString().substring(1,ContantValue.distValue.toString().length() -1));
    }
}